// 目的用axios请求、响应拦截器功能
import axios from "axios";
import { message } from 'ant-design-vue';
// import useUserStore from "@/store/modules/user";
const request = axios.create({
  // 基础路径
  baseURL: import.meta.env.VITE_API_URL || 'http://localhost:3000/api', //基础路径上会携带/api
  timeout: 5000,
});
request.interceptors.request.use((config) => {
  // const userStore = useUserStore();
  // if (userStore.token) {
  //   config.headers.token = userStore.token;
  // }
  return config;
});


request.interceptors.response.use(
  (response) => {
    return response.data;
  },
  (error) => {
    const status = error.response.status;
    switch (status) {
      case 401:
        message.error('TOKEN过期');
        break;
      case 403:
        message.error('无权访问');
        break;
      case 404:
        message.error('请求地址错误');
        break;
      case 500 | 501 | 502 | 503 | 504 | 505:
        message.error('服务器出现问题');
        break;
      default:
        message.error('网络出现问题');
        break;
    }
    return Promise.reject(error);
  },
);

export default request;